package com.ocm.commerceinteraction.util.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ocm.commerceinteraction.R;
import com.ocm.commerceinteraction.util.view.percentsupport.PercentRelativeLayout;

/**
 * Description:自定义弹出框
 *
 * @author tl
 * @Date 2014-9-23
 * @Copyright:2012-09-23
 */
public class MyAlertDialog {
    Activity context;
    AlertDialog ad;
    TextView titleView;
    EditText messageView;
    LinearLayout buttonLayout, ll_style;
    PercentRelativeLayout ll_title;

    Button btn_one, btn_two;

//	int height = 0;
//	int width = 0;

    public MyAlertDialog(Context _context) {
        this.context = (Activity) _context;

        //获取屏幕宽高
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
//		int width1 = dm.widthPixels;
//		int height1 = dm.heightPixels;
        ad = new AlertDialog.Builder(context).create();
        ad.setCanceledOnTouchOutside(true);

        LayoutInflater inflater = LayoutInflater.from(context);
        View textEntryView = inflater.inflate(R.layout.view_myalertdialog, null);
//		AlertDialog mDialog = new AlertDialog.Builder(context).create();
        ad.setView(((Activity) context).getLayoutInflater().inflate(R.layout.view_myalertdialog, null));
        ad.show();
        ad.getWindow().setContentView(textEntryView);

        //关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        window.setContentView(R.layout.view_myalertdialog);

        titleView = (TextView) window.findViewById(R.id.title);
        messageView = (EditText) window.findViewById(R.id.message);
        messageView.setFocusable(true);
        messageView.setFocusableInTouchMode(true);
        messageView.requestFocus();
        InputMethodManager inputManager =
                (InputMethodManager) messageView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(messageView, 0);
        buttonLayout = (LinearLayout) window.findViewById(R.id.buttonLayout);
        ll_style = (LinearLayout) window.findViewById(R.id.ll_style);
        btn_one = (Button) window.findViewById(R.id.btn_one);
        btn_two = (Button) window.findViewById(R.id.btn_two);
        ll_title = (PercentRelativeLayout) window.findViewById(R.id.ll_title);
        ll_title.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
//				dismiss();
            }
        });
//		//获取layout参数
//		LayoutParams rlParams =  (LayoutParams) ll_style.getLayoutParams();
//		//设置高度
//		rlParams.height = height;
//		rlParams.width = width;
//		ll_style.setLayoutParams(rlParams);
    }

    public void setTitle(int resId) {
        titleView.setText(resId);
    }

    public void setTitle(String title) {
        titleView.setText(title);
    }

    public void setMessage(int resId) {
        messageView.setText(resId);
    }

    public void setMessage(String message) {
        messageView.setText(message);
    }

    public String getMessage() {
        return messageView.getText().toString().trim();
    }

//	public void setMinusHeightOrWeith(int _width,int _height)
//	{
//		height = height-_height;
//		width = width-_width;
//		//获取layout参数
//		LayoutParams rlParams =  (LayoutParams) ll_style.getLayoutParams();
//		//设置高度
//		rlParams.height = height;
//		rlParams.width = width;
//		ll_style.setLayoutParams(rlParams);
//	}

    /**
     * 设置按钮
     *
     * @param text
     * @param listener
     */
    public void setPositiveButton(String text, final OnClickListener listener) {
        btn_two.setText(text);
        btn_two.setOnClickListener(listener);

    }

    /**
     * 设置按钮
     *
     * @param text
     * @param listener
     */
    public void setNegativeButton(String text, final OnClickListener listener) {
        btn_one.setText(text);
        btn_one.setOnClickListener(listener);
    }

    /**
     * 关闭对话框
     */
    public void dismiss() {
        ad.dismiss();
    }
}
